package generic;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-02-28
 * Time:20:38
 */
@SuppressWarnings("all")
public class Exercise2 {
    @Test
    public void test(){
        DAO<User> dao = new DAO<>();
        dao.save("1",new User(1,10,"zhangsan"));
        dao.save("2",new User(2,20,"lisi"));
        dao.save("3",new User(3,30,"wangwu"));

        Collection<User> list = dao.collection();
        System.out.println(list);

        dao.update("2",new User(5,50,"jack"));
        dao.delete("1");
        list=dao.collection();
        System.out.println(list);

        User user = dao.get("2");
        System.out.println(user);

        List<User> list1 = dao.list();
        System.out.println(list1);
    }
}
class DAO<T>{
    Map<String,T> map = new HashMap<>();
    public void save(String id,T entity){
        map.put(id,entity);
    }
    public T get(String id){
        return map.get(id);
    }
    public void update(String id,T entity){
        map.replace(id, entity);
    }
    public Collection<T> collection(){
        return map.values();
    }
    public void delete(String id){
        map.remove(id);
    }
    public List<T> list(){
        List<T> list=new ArrayList<>();
        Set<String> keySet = map.keySet();
        for (String string : keySet) {
            list.add(get(string));
        }
        return list;
    }
}
@SuppressWarnings("all")
class User{
    private int id;
    private int age;
    private String name;

    public User(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "\nUser{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}